/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2007 by Sunplus mMedia Inc.                      *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus        *
 *  mMedia Inc. All rights are reserved by Sunplus mMedia Inc.  This      *
 *  software may only be used in accordance with the corresponding        *
 *  license agreement. Any unauthorized use, duplication, distribution,   *
 *  or disclosure of this software is expressly forbidden.                *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Sunplus mMedia Inc.                                *
 *                                                                        *
 *  Sunplus mMedia Inc. reserves the right to modify this software        *
 *  without notice.                                                       *
 *                                                                        *
 *  Sunplus mMedia Inc.                                                   *
 *  19-1, Innovation First Road, Science-Based Industrial Park,           *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 *  Author: sunyong                                                *
 *                                                                        *
 **************************************************************************/
#ifndef __AUDIO_RW_H__
#define  __AUDIO_RW_H__

/**
 * @filename 	audio_rw.h
 * @brief 	The header file of audio flow
 * @author 	sunyong
 * @since 	2007-12-1
 * @date 	2007-12-1
 */

/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/
#define  RecordFrequence8khz        	1//8khz
#define  RecordFrequence11khz        	2//11.025khz
#define  RecordFrequence12khz        	3//12khz
#define  RecordFrequence16khz        	4//16khz
#define  RecordFrequence22khz        	5//22.05khz
#define  RecordFrequence32khz       	6//32khz
#define  RecordFrequence44khz       	7//44.1khz
#define  RecordFrequence48khz       	8//48khz

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
 //audio rec
 #if 0
#if (RecordFrequence8khz)
#define aud_sampleRate	(UINT32)8000
#elif(RecordFrequence11khz)
#define aud_sampleRate	(UINT32)11025
#elif(RecordFrequence12khz)
#define aud_sampleRate	(UINT32)12000
#elif(RecordFrequence16khz)
#define aud_sampleRate	(UINT32)16000
#elif(RecordFrequence22khz)
#define aud_sampleRate	(UINT32)22050
#elif(RecordFrequence32khz)
#define aud_sampleRate	(UINT32)32000
#elif(RecordFrequence44khz)
#define aud_sampleRate	(UINT32)44100
#elif(RecordFrequence48khz)
#define aud_sampleRate	(UINT32)48000
#endif
#else

#define aud_sampleRate8K	(UINT32)8000

#define aud_sampleRate11K	(UINT32)11025

#define aud_sampleRate12K	(UINT32)12000

#define aud_sampleRate16K	(UINT32)16000

#define aud_sampleRate22K	(UINT32)22050

#define aud_sampleRate32K	(UINT32)32000

#define aud_sampleRate44K	(UINT32)44100

#define aud_sampleRate48K	(UINT32)48000
#endif

#define aud_adpcmFmt 		1
#define aud_pcmFmt 			0

#define aud_blockAlign 	256

#define aud_sampleBit	16
#define aud_channel		1
#define aud_recordMode  	0

#define REC_CHUNK_SIZE	((UINT32)1024 )
#define aud_AUDBUFSIZE ((UINT32)64 * (UINT32)1024) //K_SDRAM_AviCapAudioBufSize
#define aud_AUDFRAGSIZE ((UINT32)64 * (UINT32)1024)

#define aud_Rec_Idle		0
#define aud_Rec_Run		1
#define aud_Rec_Pause	2


//audio pb
#define aud_Pb_Idle		0x00
#define aud_Pb_Run		0x01
#define aud_Pb_Pause	0x02
#define aud_Pb_FW		0x04
#define aud_Pb_RW		0x08

#define aud_pb_speed_normal 		1
#define aud_pb_speed_FW1 		2
#define aud_pb_speed_FW2 		4
#define aud_pb_speed_FW3 		8

#define aud_pb_speed_RW1 		2
#define aud_pb_speed_RW2 		4
#define aud_pb_speed_RW3 		8

#define RMV_ABORT_NOISE	28

#define AUDIO_TYPE_NORMAL		0
#define AUDIO_TYPE_VOICE		1

#define AUDIO_VOICE_MEMO_LENTH	30

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
typedef struct appAudRecCtrl_s {
			UINT8 mode;
			UINT8 recIdle;
			UINT16 fHandle;
			UINT8 name[12];
			UINT16 fileIdx;
			UINT32 freq;
							
			UINT32 SecAddr;
			UINT32 SecCnt;
			UINT32 SecNum;
			
			UINT32 DRAMAudBufCnt;

			UINT32 AudChunkCnt;
#ifdef DOS32_SUPPORT
			UINT32 freeCluster;
			UINT32 usedCluster;
#else
			UINT16 freeCluster;
			UINT16 usedCluster;
#endif
			UINT32 CurDMATagAddr;
			UINT32 CurSrcAudAddr;
			UINT32 fragBufAddr;


			UINT32 AudioAddr;
			UINT32 AVIHeadAddr;
			UINT16 headerSize;
			UINT32 dataSizeOffset;
			UINT32 time;
			//UINT8   *pHeader;   

}appAudRecCtrl_t;


typedef struct appAudPbCtrl_s {
	UINT8   idle;
	UINT8   name[12];
	UINT16 fHandle; 
	UINT32  AudBufIdx;
	UINT32  AudBufCnt;
	UINT32  AudChunckCnt;
	UINT32  PBAudioChunkBufAddr;
	UINT8 	speed;
	UINT8  headerSize;
	UINT32  time;
	UINT32  AudioAddr;
	UINT32 audInIdx;
	UINT32 audPbSize;
	UINT32 tmpCnt ;
}appAudPbCtrl_t;

/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/

/**************************************************************************
 *               F U N C T I O N    D E C L A R A T I O N S               *
 **************************************************************************/
 //audio record
UINT8 audioRecInit(void);
void audioRec(void);
void audioRecAbort(void);
void audioRecStart(void);
void audioRecStop(void); 
void audioRecPause(void);
void audioRecResume(void);
void audioRecGlobalInit(void);	
void audioRecBufSet(UINT32 addr);
void audioRecFileIdxSet(UINT16 index);
void imageVoiRecSet(UINT32 addr,UINT16 nameIdx);
UINT8 audioRecStatusGet(void);
void audioRecFreqSet(UINT8 freq) ;
void audioRecFmtSet(UINT8 fmt);
void audioRecGTSet(UINT32 );
UINT32 audioRecGTGet(void);
UINT32 audioRecTimeGet(void);

//audio play
UINT8 audioPbInit(UINT16 ID);
void audioPbSet(UINT32 audAddr,UINT32 audBufSize,UINT16 Format, UINT32 playFreq, UINT32  Block,UINT8 channels ,UINT8 ) ;
void audioPb(void);
void audioPbStart(UINT16 ID);
void audioPbStop(void) ;
void audioPbPause(void);
void audioPbResume(void);
 void audioPbFwFrRelease(void);
 void audioPbFWSpeedSet(UINT8 speed);
  void audioPbRWSpeedSet(UINT8 speed);
 UINT8 audioPbSpeedGet(void) ;
 void audioPbFW(UINT8 rate);
 void audioPbRW(UINT8 rate);
void audioPlayWaveChunck(UINT16 fHandle, UINT32 ChunckNo, UINT32 HeaderOffset, UINT32 audioBufferAddr,UINT32 chuncks) ;
void audioPbAbort(void);
void audioPbGlobalInit(void);
void audioBufSet(UINT32 addr);
UINT8 audioWAVHdrParse(UINT16 *pFormat,UINT32 *pFreq,UINT16 *pBlockAlign ,UINT16 *pChannel);
UINT32 audioReadSramLong(UINT32 addr);
void imageVoiPbSet(UINT32 addr);
UINT8 audiopbStatusGet(void) ;
void audioPbMsGet(UINT32 *pTime);
UINT32 audioPbIndexGet(void) ;
void audioPbGTSet(UINT32 pausetime);
UINT32 audioPbGTGet(void);
void audioRecTypeSet(UINT8 type);
UINT8 audioRecTypeGet(void);

#endif

